package com.tanhua.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.Intercepter.UserHolder;
import com.tanhua.config.template.SendMessageTemeplate;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.exception.BusinessExecption;
import com.tanhua.model.domain.*;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private UserApi userApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private SettingsApi settingsApi;

    @Autowired
    SendMessageTemeplate sendMessageTemeplate;

    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 查询通用设置
     *
     * @return
     */
    public SettingsVo Settings() {
        Long id = UserHolder.getId();//获取userid
        SettingsVo vo = new SettingsVo();
        vo.setId(id);
        String mobile = UserHolder.getMobile();//获取手机号
        vo.setPhone(mobile);
        Question question = questionApi.FindById(id);
        String txt = question.getTxt() == null ? "一起秃头吧" : question.getTxt();
        vo.setStrangerQuestion(txt);
        Settings settings = settingsApi.FindById(id);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    /**
     * 保存问题
     *
     * @param content
     */
    public void SaveQuestion(String content) {
        Long id = UserHolder.getId();
        Question question = questionApi.FindById(id);
        if (question == null) {
            //若为空则新增
            question = new Question();
            question.setUserId(id);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //存在则修改
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    /**
     * 修改用户通知设置
     * @param map
     */
    public void updateSetting(Map map) {
        Boolean likeNotification = (Boolean) map.get("likeNotification");//推送喜欢通知
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");//推送评论通知
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");//推送公告通知
        Long id = UserHolder.getId();
        Settings settings = settingsApi.FindById(id);
        //为空则新增，存在则修改数据
        if (settings==null){
            settings = new Settings();
            settings.setUserId(id);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        }else {
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
    }

    /**
     * 黑名单查看
     * @return
     */
    public PageResult BlackListSelect(int page ,int size) {
        Long userId = UserHolder.getId();
        //查询黑名单分页列表
        IPage<UserInfo> blackLists = blackListApi.FindByUserId(userId,page,size);
        //转换成PageResult对象返回
        PageResult pageResult = new PageResult(page,size, (int) blackLists.getTotal(),blackLists.getRecords());
        return pageResult;
    }

    /**
     * 删除黑名单
     * @param blackUserd
     */
    public void deleteBlackList(Long blackUserd) {
        blackListApi.deleteBlackList(blackUserd);
    }

    /**
     * 修改手机号- 1 发送短信验证码
     */
    public void sendVerificationCode() {
        String mobile = UserHolder.getMobile();
        String code = RandomStringUtils.randomNumeric(6);
        code ="123456";
        sendMessageTemeplate.sms(code,mobile);
        //将验证码存入redis
        redisTemplate.opsForValue().set("CHECK_CODE_"+mobile,code);
    }

    /**
     * 修改手机号 - 2 校验验证码
     * @param verificationCode 验证码
     * @return
     */
    public Map checkVerificationCode(String verificationCode) {
        Map map = new HashMap();
        String code = (String) redisTemplate.opsForValue().get("CHECK_CODE_" + UserHolder.getMobile());

        if (code.isEmpty() || ! code.equals(verificationCode)){
            map.put("verification",false);
            throw new BusinessExecption(ErrorResult.loginError());
        }
        //验证成功，删除redis
        redisTemplate.delete("CHECK_CODE_" + UserHolder.getMobile());
        map.put("verification",true);
        return map;
    }

    /**
     * 修改手机号 - 3 保存
     * @param mobile
     */
    public void updatePhone(String mobile) {
        Long id = UserHolder.getId();
        User user = new User();
        user.setId(id);
        user.setMobile(mobile);
        UserHolder.setUserThreadLocal(user);
        userApi.update(user);
    }
}
